package com.niodata.dp.task.dao.handler;

import com.niodata.dp.log.LogUtil;
import com.niodata.dp.mybatis.DpMybatisTypeHandler;
import com.niodata.dp.task.entity.JobNodeStyleInfo;
import com.niodata.dp.util.JsonUtil;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.log4j.Logger;

public class JobNodeStyleInfoHandler extends BaseTypeHandler<JobNodeStyleInfo>
    implements DpMybatisTypeHandler {

  private static final Logger logger = Logger.getLogger(JobNodeStyleInfoHandler.class);

  @Override
  public JdbcType getJdbcType() {
    return JdbcType.LONGVARCHAR;
  }

  @Override
  public Class getTypeHandlerClass() {
    return JobNodeStyleInfoHandler.class;
  }

  @Override
  public Class getJavaClass() {
    return JobNodeStyleInfo.class;
  }

  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, JobNodeStyleInfo parameter,
      JdbcType jdbcType) throws SQLException {
    try {
      ps.setString(i, JsonUtil.toJson(parameter));
    } catch (IOException e) {
      LogUtil.logStackTrace(logger, e);
    }
  }

  @Override
  public JobNodeStyleInfo getNullableResult(ResultSet rs, String columnName) throws SQLException {
    try {
      return JsonUtil.fromJson(JobNodeStyleInfo.class, rs.getString(columnName));
    } catch (IOException e) {
      LogUtil.logStackTrace(logger, e);
      return null;
    }
  }

  @Override
  public JobNodeStyleInfo getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    try {
      return JsonUtil.fromJson(JobNodeStyleInfo.class, rs.getString(columnIndex));
    } catch (IOException e) {
      LogUtil.logStackTrace(logger, e);
      return null;
    }
  }

  @Override
  public JobNodeStyleInfo getNullableResult(CallableStatement cs, int columnIndex)
      throws SQLException {
    return null;
  }
}
